import ico from '@/assets/icons/bbb.png'

/**
 * @name dateFormat
 * @author darcrand
 * @param {Object|Number} date 日期对象|时间戳
 * @param {String} formate 日期格式
 */

function dateFormate(date, formate = 'yyy-mmm-ddd 星期www hh:mm:ss') {
  if (!date) {
    throw new Error('first param "date" is missing')
  } else if (!(date instanceof Date || date > 0)) {
    throw new Error('first param "date" should be a "object Date()" or "timeStamp"')
  }
  date = date instanceof Date ? date : new Date(date)
  const d = {
    yyy: date.getFullYear(),
    mmm: date.getMonth() + 1,
    ddd: date.getDate(),
    www: ['日', '一', '二', '三', '四', '五', '六'][date.getDay()],
    hh: date.getHours(),
    mm: date.getMinutes(),
    ss: date.getSeconds()
  }
  const zero = num => (num > 9 ? num : `0${num}`)
  let formateStr = formate
  for (const key in d) {
    if (d.hasOwnProperty(key)) {
      const reg = new RegExp(`${key}`)
      formateStr = formateStr.replace(reg, key === 'www' ? d[key] : zero(d[key]))
    }
  }
  return formateStr
}

/**
 * @name nav-to
 * @desc 模拟跳转到指定页面;可传参数
 * @param {String|Object} to 页面url | 带参数的url
 * @param {String} to.name 页面url
 * @param {Object} to.query 页面参数
 */

function navTo(to) {
  const type = (typeof to).toLowerCase()
  const prefix = '/pages/'
  const suffix = '/main'
  let url = null
  if (type === 'string') {
    url = `${prefix}${to}${suffix}`
  } else if (type === 'object') {
    if (to.name && to.query) {
      let str = '?'
      for (const [k, v] of Object.entries(to.query)) {
        str += `${k}=${v}&`
      }
      str = str.slice(0, -1)
      url = `${prefix}${to.name}${suffix}${str}`
    } else if (to.name) {
      url = `${prefix}${to.name}${suffix}`
    }
  }
  wx.navigateTo({ url })
}

/**
 * @param {Number} num - 要处理的数值
 * @param {Number} count - 小数点后保留的位数
 * @param {String} type - 小数位舍入方式,可选['round','ceil','floor']
 */
function numberFixed(num = 0, { count = 2, type = 'round' } = {}) {
  const power = Math.pow(10, count)
  return Math[type](num * power) / power
}

/**
 * @desc 收支的方式
 * @param {String} text 方式的名称
 * @param {Object} icon 对应的图标对象
 */

const wayMap = [
  [
    { id: '0-1', text: '工资', ico },
    { id: '0-2', text: '红包', ico },
    { id: '0-3', text: '生活费', ico },
    { id: '0-4', text: '奖金', ico },
    { id: '0-5', text: '报销', ico },
    { id: '0-6', text: '兼职', ico },
    { id: '0-7', text: '借款', ico },
    { id: '0-8', text: '投资', ico },
    { id: '0-9', text: '理财', ico },
    { id: '0-10', text: '其他', ico }
  ],
  [
    { id: '1-1', text: '餐饮', ico },
    { id: '1-2', text: '零食', ico },
    { id: '1-3', text: '交通', ico },
    { id: '1-4', text: '购物', ico },
    { id: '1-5', text: '服饰', ico },
    { id: '1-6', text: '居住', ico },
    { id: '1-7', text: '娱乐', ico },
    { id: '1-8', text: '医疗', ico },
    { id: '1-9', text: '教育', ico },
    { id: '1-10', text: '人情', ico },
    { id: '1-11', text: '通讯', ico },
    { id: '1-12', text: '居家', ico },
    { id: '1-13', text: '健身', ico },
    { id: '1-14', text: '其他', ico }
  ]
]

/**
 * @name toDailyList
 * @desc 将原始列表转化为按天分组的二维数组
 * @param {Array} list 原始列表
 */

function toDailyList(list) {
  return list.reduce((pre, curr) => {
    if (pre.length && pre[pre.length - 1].length) {
      const itemDate = new Date(curr.date).getDate()
      const theDate = new Date(pre[pre.length - 1][0].date).getDate()
      if (itemDate === theDate) {
        pre[pre.length - 1].push(curr)
        return pre
      } else {
        return [...pre, [curr]]
      }
    } else {
      return [...pre, [curr]]
    }
  }, [])
}

/**
 * @desc hsla 转为 rgba
 * @param {Number} h 色相 [0-360]
 * @param {Number} s 饱和度 [0-1]
 * @param {Number} l 亮度 [0-1] 0.5时为正常
 * @param {Number} a 透明度 [0-1]
 */
function hsla2rgba(h = 0, s = 1, l = 0.5, a = 1) {
  let r, g, b
  let deg = h % 360
  // 将色相转化为[0-1]的数值
  deg = deg < 0 ? 360 + deg : deg
  const num = Math.floor(1000 * deg / 360) / 1000

  if (s === 0) {
    r = g = b = l
  } else {
    const hue2rgb = (p, q, t) => {
      if (t < 0) t += 1
      if (t > 1) t -= 1
      if (t < 1 / 6) return p + (q - p) * 6 * t
      if (t < 1 / 2) return q
      if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6
      return p
    }

    const q = l < 0.5 ? l * (1 + s) : l + s - l * s
    const p = 2 * l - q
    r = hue2rgb(p, q, num + 1 / 3)
    g = hue2rgb(p, q, num)
    b = hue2rgb(p, q, num - 1 / 3)
  }

  return `rgba(${Math.floor(r * 255)},${Math.floor(g * 255)},${Math.floor(b * 255)},${a})`
}

/**
 * @name MonthArr
 * @desc 日历月份的二维数组
 * @param {Number} year
 * @param {NUmber} month
 */
function MonthArr(year, month) {
  const prevMonthLength = new Date(year, month, 0).getDate()
  const currMonthLength = new Date(year, month + 1, 0).getDate()
  const beforeDayLength = new Date(year, month, 1).getDay()
  const afterDayLength = 6 - new Date(year, month + 1, 0).getDay()
  let baseArr = []

  for (let i = 0; i < beforeDayLength; i++) {
    baseArr.unshift({
      date: new Date(year, month - 1, prevMonthLength - i).getTime(),
      label: prevMonthLength - i,
      isCurrMonth: false
    })
  }
  for (let i = 0; i < currMonthLength; i++) {
    baseArr.push({ date: new Date(year, month, i + 1).getTime(), label: i + 1, isCurrMonth: true })
  }
  for (let i = 0; i < afterDayLength; i++) {
    baseArr.push({ date: new Date(year, month + 1, i + 1).getTime(), label: i + 1, isCurrMonth: false })
  }

  const resArr = baseArr.reduce((pre, curr, index) => {
    if (index % 7) {
      const arr = [...pre]
      arr[arr.length - 1].push(curr)
      return arr
    } else {
      return [...pre, [curr]]
    }
  }, [])

  return resArr
}

export { navTo, numberFixed, wayMap, dateFormate, toDailyList, hsla2rgba, MonthArr }
